package me.pplay.playtalk.activity.setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.data.CommonData;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;

public class SettingSystemAvatarActivity extends BaseActivity {
	private TpChat tpChat;
	private ImageButton btn_back;
	private GridView gridView;
	private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_setting_systemavatar);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.viewmember_btnback));
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat = appState.getTpChat();

		btn_back = (ImageButton)findViewById(R.id.systemavatar_btnback);
		gridView = (GridView)findViewById(R.id.systemavatar_gv);
		
		for(int i = 1;i < 20;i++){
			Map<String, Object> item = new HashMap<String, Object>();
			try {
				int id = R.drawable.class.getDeclaredField("avatar"+i).getInt(R.drawable.class);
				item.put("avatarId", R.drawable.class.getDeclaredField("avatar"+i).getInt(R.drawable.class));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) { 
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			list.add(item);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, 
				list, 
				R.layout.layout_griditem, 
				new String[]{"avatarId"}, 
				new int[]{R.id.systemavatar_iv_item});
		gridView.setAdapter(simpleAdapter);
		clickEvents();
	}	
	
	private void clickEvents() {
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SettingSystemAvatarActivity.this.finish();
			}
		});
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				TPBuddyinfo buddyinfo = new TPBuddyinfo();
				buddyinfo.setAvatarid(position+CommonData.AVATAR_BASE_ID);
				tpChat.tp_chat_set_info(buddyinfo);
					if(null != Globals.myInfo){
						Globals.myInfo.setAvatarid(position+CommonData.AVATAR_BASE_ID);
					}
				SettingSystemAvatarActivity.this.finish();
			}
			
		});
		
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
